/*
 * @Author: silence
 * @Date: 2023-07-07 11:56:11
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2023-10-25 15:50:16
 * @Description: 启动页
 */

import 'package:ems_neutral_app/api/request/UserRequest.dart';
import 'package:ems_neutral_app/base/SilScaffold.dart';
import 'package:ems_neutral_app/common/interfaceInstance/FutureBuilderInterfaceInstance.dart';
import 'package:ems_neutral_app/pages/PowerStation/Add/PowerStationAddPage.dart';
import 'package:flutter/material.dart';
import 'package:sil_base_widget_package/sil_base_widget_package.dart';
import 'package:sil_utils_package/tools/SilTools.dart';

import '../../api/R.dart';
import '../../base/SilTabbarPage.dart';
import '../../bloc/userbloc/user_bloc.dart';
import '../../entities/UserEntity.dart';
import '../../utils/color/ColorUtils.dart';
import '../../utils/router/ARouter.dart';
import '../Login/LoginGuidPage.dart';

class LaunchPage extends StatefulWidget {
  const LaunchPage({super.key});

  @override
  State<LaunchPage> createState() => _LaunchPageState();
}

class _LaunchPageState extends State<LaunchPage>
    with FutureBuilderInterfaceInstance {
  late Widget _widget;

  var _future = null;

  @override
  void initState() {
    super.initState();
    _future = getData();
  }

  getData() async {
    if (UserBlocTool.isLogin() == false) {
      return;
    }

    R r = await api_userInfo_info();
    if (r.code != 200) {
      ARoute.replacePage(context, LoginGuidPage());
    }
    return r.data;
  }

  @override
  Widget build(BuildContext context) {
    return SilScaffold(
        sideMargin: 0,
        navBarHide: true,
        stateBarHide: true,
        body: futureBuilderWidget());
  }

  futureBuilderWidget() {
    return SilFutureBuilderWidget(
      future: _future,
      mainWidget: (value) {
        return mainWidget();
      },
      reloadFunc: () {
        setState(() {
          _future = getData();
        });
      },
      interface: this,
    );
  }

  mainWidget() {
    /// 默认一般是启动图/启动动画最后一帧
    // _widget = Container(
    //   color: ColorUtils.color_bg,
    //   child: SilImage(
    //     'imgs/common/kaiji_jingtai.jpg',
    //     height: MediaQuery.of(context).size.height,
    //     width: MediaQuery.of(context).size.width,
    //   ),
    // );

    UserEntity? userEntity = UserBlocTool.userEntity;
    if (SilTools.stringIsEmpty(userEntity?.powerRecordId) ||
        userEntity?.powerRecordId == '0') {
      _widget = PowerStationAddPage(navBarHide: true);
    } else {
      _widget = SilTabbar();
    }

    return _widget;
  }
}
